package lispinterpreter;

/**
 * 
 * @author setian
 */
public class LispAtom extends LispList {

    public static final int ATOM_TYPE_BOOLEAN = 1;
    public static final int ATOM_TYPE_FLOAT = 0;
    public static final int ATOM_TYPE_PARENTHESIS = 3;
    public static final int ATOM_TYPE_STRING = 2;

    public static int typeIdentifier(String cadena) {
	String numeros = "1234567890";
	String punto = ".";
	String btrue = "T";
	String bfalse = "NIL";
	boolean paso = false;
	for (int i = 0; i < cadena.length(); i++) {
	    for (int j = 0; j < numeros.length(); j++) {
		if (cadena.charAt(i) == numeros.charAt(j)
			|| cadena.charAt(i) == punto.charAt(0)) {
		    paso = true;
		}
	    }
	}
	if (paso) {
	    int contador = 0;
	    for (int i = 0; i < cadena.length(); i++) {
		if (cadena.charAt(i) == punto.charAt(0)) {
		    contador++;
		}
	    }
	    if (contador == 1 || contador == 0) {
		return LispAtom.ATOM_TYPE_FLOAT; // float
	    }
	}
	if ((cadena.length() == 1 && cadena.charAt(0) == btrue.charAt(0))
		|| (cadena.length() == 3 && cadena.equals(bfalse))) {
	    return LispAtom.ATOM_TYPE_BOOLEAN; // boolean
	}
	return LispAtom.ATOM_TYPE_STRING; // String

    }

    public boolean batom;
    public char catom;
    public float fatom;
    public String satom = null;

    // Atributos
    private final int type;

    public LispAtom(boolean booleano) {
	isAtom = true;
	batom = booleano;
	type = ATOM_TYPE_BOOLEAN;
    }

    public LispAtom(float num) {
	isAtom = true;
	fatom = num;
	type = ATOM_TYPE_FLOAT;
    }

    // Constructor
    public LispAtom(String cadena) {
	isAtom = true;
	if (cadena.length() == 1 && (cadena.equals("(") || cadena.equals(")"))) {
	    type = ATOM_TYPE_PARENTHESIS;
	    if (cadena.equals("(")) {
		catom = '(';
	    } else {
		catom = ')';
	    }
	} else {
	    satom = cadena;
	    type = ATOM_TYPE_STRING;
	}
    }

    public int getType() {
	return type;
    }

    /**
     * Devuelve en contenido del átmo. Puede ser un string, boolean o float
     */
    public void printAtom() {
	if (type == ATOM_TYPE_BOOLEAN) {
	    if (batom) {
		System.out.println("T ");
	    } else {
		System.out.println("NIL");
	    }
	} else {
	    if (type == ATOM_TYPE_FLOAT) {
		System.out.println("" + fatom);
	    } else {
		System.out.println(satom);
	    }
	}
    }

}